﻿using System;
using UnityEngine;

[DisallowMultipleComponent]
public class DragListener : MonoBehaviour
{
    public float dragMax = 30.0f;
    public float dragTime = 0.3f;
    public Action<bool> CallBack;

    private bool m_BeginTouch;
    private float m_DragDiff;
    private float m_DragTimer;

    void OnDrag(Vector2 vector)
    {
        if (m_BeginTouch)
        {
            m_DragDiff += vector.x;
        }
    }

    void OnPress(bool isPressed)
    {
        if (isPressed)
        {
            if (Time.realtimeSinceStartup - m_DragTimer >= 0.3f)
            {
                m_DragTimer = Time.realtimeSinceStartup;
                m_BeginTouch = true; m_DragDiff = 0;
            }
        }
        else
        {
            dragMax = Screen.width / 8; m_BeginTouch = false;
            if (Mathf.Abs(m_DragDiff) >= dragMax)
            {
                if (m_DragDiff < -dragMax)
                {
                    CallBack(true);
                }
                else if (m_DragDiff > dragMax)
                {
                    CallBack(false);
                }
            }
        }
    }
}